udebug: use proper libudebug API
authorFelix Fietkau <[email protected]>
Wed, 28 May 2025 11:10:13 +0000 (13:10 +0200)
committerFelix Fietkau <[email protected]>
Wed, 28 May 2025 11:10:14 +0000 (13:10 +0200)
Replace handwritten code for enabling debug ring buffers

Signed-off-by: Felix Fietkau <[email protected]>
main.c
ubus.c
ubus.h

diff --git a/main.c b/main.c
index 752e78adf06589948d14f87898a913da931cc91a..521a2664668320f571784216e90d9e3d9f8717c1 100644 (file)
--- a/main.c
+++ b/main.c
@@ -26,7 +26,6 @@
 #include <netinet/in.h>
 #include <arpa/nameser.h>
 
-#include <udebug.h>
 #include <libubus.h>
 #include <libubox/uloop.h>
 
@@ -42,18 +41,30 @@ int cfg_proto = 0;
 int cfg_no_subnet = 0;
 
 static struct udebug ud;
-static struct udebug_buf udb;
-static bool udebug_enabled;
+static struct udebug_buf udb_log;
+static const struct udebug_buf_meta meta_log = {
+       .name = "umdns_log",
+       .format = UDEBUG_FORMAT_STRING,
+};
+
+static struct udebug_ubus_ring rings[] = {
+       {
+               .buf = &udb_log,
+               .meta = &meta_log,
+               .default_entries = 1024,
+               .default_size = 64 * 1024,
+       }
+};
 
 static void
 umdns_udebug_vprintf(const char *format, va_list ap)
 {
-       if (!udebug_enabled)
+       if (!udebug_buf_valid(&udb_log))
                return;
 
-       udebug_entry_init(&udb);
-       udebug_entry_vprintf(&udb, format, ap);
-       udebug_entry_add(&udb);
+       udebug_entry_init(&udb_log);
+       udebug_entry_vprintf(&udb_log, format, ap);
+       udebug_entry_add(&udb_log);
 }
 
 void umdns_udebug_printf(const char *format, ...)
@@ -65,27 +76,11 @@ void umdns_udebug_printf(const char *format, ...)
        va_end(ap);
 }
 
-void umdns_udebug_set_enabled(bool val)
-{
-       static const struct udebug_buf_meta meta = {
-               .name = "umdns_log",
-               .format = UDEBUG_FORMAT_STRING,
-       };
-
-       if (udebug_enabled == val)
-               return;
-
-       udebug_enabled = val;
-       if (!val) {
-               udebug_buf_free(&udb);
-               udebug_free(&ud);
-               return;
-       }
 
-       udebug_init(&ud);
-       udebug_auto_connect(&ud, NULL);
-       udebug_buf_init(&udb, 1024, 64 * 1024);
-       udebug_buf_add(&ud, &udb, &meta);
+void umdns_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+                        bool enabled)
+{
+       udebug_ubus_apply_config(&ud, rings, ARRAY_SIZE(rings), data, enabled);
 }
 
 static void
@@ -100,6 +95,8 @@ main(int argc, char **argv)
        int ch, ttl;
 
        uloop_init();
+       udebug_init(&ud);
+       udebug_auto_connect(&ud, NULL);
 
        while ((ch = getopt(argc, argv, "t:i:d46n")) != -1) {
                switch (ch) {
diff --git a/ubus.c b/ubus.c
index 031955455a9db4bfef494fb3f62d29ffc821d10e..813db9ead7b3ed9619bcc602bb6120abe25e7dd4 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -28,8 +28,8 @@
 #include "interface.h"
 
 static struct ubus_auto_conn conn;
+static struct udebug_ubus udebug;
 static struct blob_buf b;
-static struct ubus_subscriber udebug_sub;
 
 static int
 umdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
@@ -369,87 +369,16 @@ static struct ubus_object umdns_object = {
        .n_methods = ARRAY_SIZE(umdns_methods),
 };
 
-static struct blob_attr *
-find_attr(struct blob_attr *attr, const char *name, enum blobmsg_type type)
-{
-       struct blobmsg_policy policy = { name, type };
-       struct blob_attr *ret;
-
-       if (!attr)
-               return NULL;
-
-       blobmsg_parse_attr(&policy, 1, &ret, attr);
-
-       return ret;
-}
-
-static void
-umdns_udebug_config_cb(struct blob_attr *data)
-{
-       enum {
-               CFG_ATTR_ENABLED,
-               __CFG_ATTR_MAX
-       };
-       static const struct blobmsg_policy policy[__CFG_ATTR_MAX] = {
-               [CFG_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_STRING },
-       };
-       struct blob_attr *tb[__CFG_ATTR_MAX];
-       bool en;
-
-       data = find_attr(data, "service", BLOBMSG_TYPE_TABLE);
-       data = find_attr(data, "umdns", BLOBMSG_TYPE_TABLE);
-       if (!data)
-               return;
-
-       blobmsg_parse_attr(policy, __CFG_ATTR_MAX, tb, data);
-       if (!tb[CFG_ATTR_ENABLED])
-               return;
-
-       en = !!atoi(blobmsg_get_string(tb[CFG_ATTR_ENABLED]));
-       umdns_udebug_set_enabled(en);
-}
-
-static int
-umdns_udebug_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
-                       struct ubus_request_data *req, const char *method,
-                       struct blob_attr *msg)
-{
-       umdns_udebug_config_cb(msg);
-
-       return 0;
-}
-
-static void
-umdns_udebug_req_cb(struct ubus_request *req, int type, struct blob_attr *msg)
-{
-       umdns_udebug_config_cb(msg);
-}
-
-static bool
-umdns_udebug_sub_cb(struct ubus_context *ctx, struct ubus_subscriber *sub,
-                    const char *path)
-{
-       return !strcmp(path, "udebug");
-}
-
-
 static void
 ubus_connect_handler(struct ubus_context *ctx)
 {
-       uint32_t id;
        int ret;
 
        ret = ubus_add_object(ctx, &umdns_object);
        if (ret)
                fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
 
-       udebug_sub.cb = umdns_udebug_notify_cb;
-       udebug_sub.new_obj_cb = umdns_udebug_sub_cb;
-       ubus_register_subscriber(&conn.ctx, &udebug_sub);
-       if (ubus_lookup_id(&conn.ctx, "udebug", &id) == 0) {
-               ubus_subscribe(&conn.ctx, &udebug_sub, id);
-               ubus_invoke(&conn.ctx, id, "get_config", NULL, umdns_udebug_req_cb, NULL, 1000);
-       }
+       udebug_ubus_init(&udebug, ctx, "umdns", umdns_udebug_config);
 }
 
 void
diff --git a/ubus.h b/ubus.h
index e880b7365f6aeb90d2fa11d5c7daec3a076b4884..47968250022cd59874abeb4d2e1a2a03b3a1d808 100644 (file)
--- a/ubus.h
+++ b/ubus.h
 #ifndef _UBUS_H__
 #define _UBUS_H__
 
+#include <udebug.h>
+
 extern void ubus_startup(void);
 extern int ubus_service_list(ubus_data_handler_t cb);
+void umdns_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+                        bool enabled);
 
 #endif